package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.MedicinerDTO;
import com.api.shopapi.entity.dto.MedicinerStaffRelDTO;
import com.api.shopapi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 */
@RestController
@RequestMapping("MedicinerStaffRel")
public class MedicinerStaffRelController extends BaseController {

    @Autowired
    @Qualifier("MedicinerStaffRelServiceImplOne")
    private MedicinerStaffRelService medicinerStaffRelService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    /**
     * 医生关联后台账户
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicinerStaffRel/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            medicinerStaffRelService.insert(jsonObject);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/MedicinerStaffRel/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //查看绑定详情列表
    @RequestMapping(value = "RelDetails", method = RequestMethod.POST)
    public MyResult RelDetails(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String medicinerCode = jsonObject.getString("medicinerCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicinerStaffRel/RelDetails");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            List<MedicinerStaffRelDTO> medicinerStaffRelDTOList = medicinerStaffRelService.getListByMedicinerCode(medicinerCode);
            for (MedicinerStaffRelDTO medicinerStaffRelDTO:medicinerStaffRelDTOList){
                Staff staff = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                medicinerStaffRelDTO.setStaff(staff);
            }
            return success(medicinerStaffRelDTOList);
        } catch (Exception e) {
            insertError("/MedicinerStaffRel/RelDetails", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //解除绑定
    @RequestMapping(value = "Del", method = RequestMethod.POST)
    public MyResult del(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String medicinerCode = jsonObject.getString("medicinerCode");
            String medicinerStaffCode = jsonObject.getString("medicinerStaffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicinerStaffRel/RelDetails");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            medicinerStaffRelService.delByMedicinerCodeAndMedicinerStaffCode(medicinerCode,medicinerStaffCode);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/MedicinerStaffRel/RelDetails", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //编辑
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String medicinerCode = jsonObject.getString("medicinerCode");
            JSONArray listStaff = jsonObject.getJSONArray("listStaff");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/CommendCenterStaffRel/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            //删除老的关系
            List<MedicinerStaffRelDTO> listStaffRel = medicinerStaffRelService.getListByMedicinerCode(medicinerCode);
            for(MedicinerStaffRelDTO medicinerStaffRelDTO : listStaffRel){
                medicinerStaffRelService.delete(medicinerStaffRelDTO.getMedicinerStaffRelCode());
            }

            //增加新的关系
            for (int k = 0; k < listStaff.size(); k++) {

                JSONObject staffTemp = listStaff.getJSONObject(k);

                MedicinerStaffRel medicinerStaffRel = new MedicinerStaffRel();
                medicinerStaffRel.setMedicinerStaffRelCode(UUID.randomUUID().toString());
                medicinerStaffRel.setMedicinerCode(medicinerCode);
                medicinerStaffRel.setStaffCode(staffTemp.getString("staffCode"));
                medicinerStaffRelService.insert2(medicinerStaffRel);
            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/CommendCenterStaffRel/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
